/*This file is part of Super Luigi Bros.

    Super Luigi Bros is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Super Luigi Bros is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Super Luigi Bros.  If not, see <http://www.gnu.org/licenses/>.
 */

package luigibros.logic.thread;

import java.awt.event.KeyEvent;
import java.util.ArrayList;

import luigibros.logic.Luigi;
import luigibros.logic.Collidable;
import luigibros.logic.ChangeManager;
import luigibros.logic.ScreenOccupant;

/**Thread for movement
 * of Mario/Luigi specifically
 * @author goldengrif
 * @author krkini7 --ME TOO!(Some run corrections for improved workability with other classes)
 */
public class MarioMoveThread implements ChangeManager
{
        private KeysStates state;
        private Thread mover;
        private Luigi luigi;
        private boolean collision;
        private volatile boolean isAlive;
        private ArrayList<ScreenOccupant[]> collisions;
        
        public MarioMoveThread(KeysStates keys, Luigi player)
        {
                this.state = state;
                this.luigi = player;
                mover = new Thread(this);
                mover.start();
        }
        
        //figure out how to call to Keertan's code?
        public void run() 
        {
                //May need try {
                //} catch (InterruptedException ex) {
                //thread.interrupt();
                //}
                
                
                //Needs methods from extension of other classes
                while (isAlive)
                {
                        
                        if (state.getUpPressed())
                        {
                                //Will call luigi's up move method method
                                luigi.isJump(true);
                        }
                        
                        else if (state.getDownPressed())
                        {
                                luigi.isJump(false);
                        }
                                
                        else if (state.getLeftPressed())
                        {
                                luigi.setDirection(false);
                        }
                                        
                        else if (state.getRightPressed())
                        {
                                luigi.setDirection(true);
                        }
                                
                        else if (state.getShootPressed())
                        {
                                luigi.shoot();
                        }
                        
                        else if (state.getSpacePressed())
                        {
                                luigi.isJump(true);
                        }
                        
                        //Depends on collision filter to determine if mario is alive or not.
                        if (luigi.getVisible())
                        {
                                killThread();
                        }
                }

        }
        
        //get where the player is at in x and y coordinates.
        //called again after movement to see how far player went
        //determines change in x and change in y

        public double getDeltaX()
        {
                double moveCount = 0;
                //Some method to get luigi's x starting position
                //Will be used to determine where luigi is at in the end
                double start = luigi.getX();
                
                //Some method call to get whether or not luigi is moving?
                while(luigi.moving)
                {
                        moveCount++;
                        //Some array with screen occupants is used?
                        addCollisions(collisions);
                }
                
                double xChange = moveCount - start;
                
                
                
                return xChange;
        }
        
        public double getDeltaY()
        {
                double heightCount = 0;
                //Some method to get luigi's y starting position
                //Will be used to determine where luigi is at in the end.
                double level = luigi.getY();
                
                //Some method to determine if luigi is still in air
                //Must stop upon landing on some object at height ?
                while (luigi.airTime)
                {
                        heightCount++;
                }
                
                double yChange = heightCount - level;
                
                addCollissions();
                
                return yChange;
                
        }
        
        //field array collisions to store collisions happening at the moment for this object
        //After registering DeltaX and DeltaY, process collisions
        public void addCollissions ()
        {
        	
        	
        	 	//Some array with screen occupants is used?
        		//But does process if collisions have occured or not
                collision = luigi.processCollisions(collisions);
               
        }
        
        public void move()
        {
        	luigi.setX(luigi.getX() + getDeltaX());
        	luigi.setY(luigi.getY() + getDeltaY());
        }
        
        
        public void killThread()
        {
                this.isAlive = false;
        }      
}

